package cn.com.dashboard.service;

import cn.com.dashboard.dao.ZrRulesMapper;
import cn.com.dashboard.entity.ZrRules;
import cn.com.dashboard.model.*;
import cn.com.dashboard.model.rules.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jiaming
 */
@Service
public class RuleServiceImpl implements RuleService {

    protected static final Logger log = LogManager.getLogger();

    @Autowired
    private ZrRulesMapper rulesMapper;

    @Override
    public CommonResult<Boolean> addRule(AddRuleRequest request) {
        CommonResult result = new CommonResult();
        try {
            ZrRules rules = new ZrRules();
            BeanUtils.copyProperties(request, rules);
            rulesMapper.insert(rules);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setFailed("500", "插入失败");
        }
        return result;
    }

    @Override
    public CommonResult<Boolean> editRule(EditRuleRequest request) {
        CommonResult result = new CommonResult();
        try {
            ZrRules rules = new ZrRules();
            BeanUtils.copyProperties(request, rules);
            int rows = rulesMapper.updateByPrimaryKey(rules);
            if (rows <= 0) {
                result.setFailed("500", "更新失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setFailed("500", "更新失败");
        }
        return result;
    }

    @Override
    public CommonResult<Boolean> deleteRule(DeleteRuleRequest request) {
        CommonResult result = new CommonResult();
        if (request.getId() != null) {
            rulesMapper.deleteByPrimaryKey(request.getId());
        } else {
            result.setFailed("500", "删除失败");
        }
        return result;
    }

    @Override
    public CommonResult<RulesDetailResponse> details(QueryRulesDetails request) {
        Integer id = request.getId();
        if (id == null) {
            return CommonResult.failed("400", "参数错误");
        }
        CommonResult result = new CommonResult();
        ZrRules zrRules = rulesMapper.selectByPrimaryKey(id);
        RulesDetailResponse response = new RulesDetailResponse();
        if (zrRules != null) {
            response.setRules(zrRules);
            result.setData(response);
        } else {
            result.setFailed("1001", "未查询到规则");
        }
        return result;
    }

    @Override
    public CommonPage<ZrRules> queryRulesList(QueryRulesListRequest request) {
        ZrRules condition = new ZrRules();
        BeanUtils.copyProperties(request, condition);
        PageHelper.startPage(request.getPageNo(), request.getPageSize());
        List<ZrRules> rulesList = rulesMapper.selectRulesList(condition);
        PageInfo<ZrRules> pageInfo = new PageInfo<>(rulesList);
        return CommonPage.restPage(pageInfo.getList(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getPageSize());
    }

}
